package com.huatai.bpmn.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.bpmn.entity.RoleEntity;
import com.huatai.bpmn.entity.TbUserDeploymentEntity;
import com.huatai.bpmn.entity.dto.PageDto;
import com.huatai.bpmn.entity.vo.UserRoleVo;
import com.huatai.bpmn.service.RoleService;
import com.huatai.bpmn.service.TbUserDeploymentService;
import com.huatai.bpmn.service.UserService;
import com.huatai.common.api.R;
import com.huatai.common.util.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: nzj
 * @Date: 2023/7/13 11:51
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户角色表", tags = "用户角色表")
public class UserRoleController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private TbUserDeploymentService tbUserDeploymentService;


	/**
	 * 获取用户列表
	 */
	@PostMapping("getUserList")
	@ApiOperation("获取用户列表")
	@ApiOperationSupport(includeParameters = {
		"pageDto.page",
		"pageDto.limit"
	})
	public R<Map<String, Object>> getUserList(@RequestBody PageDto pageDto){
		return userService.getUserList(pageDto);
	}

	/**
	 * 获取角色列表
	 */
	@PostMapping("getRoleList")
	@ApiOperation("获取角色列表")
	@ApiOperationSupport(includeParameters = {
		"pageDto.page",
		"pageDto.limit"
	})
	public R<Map<String, Object>> getRoleList(@RequestBody PageDto pageDto){
		return userService.getRoleList(pageDto);
	}

	/**
	 * 添加角色
	 */
	@PostMapping("/addRole")
	@ApiOperationSupport(includeParameters = {
		"roleEntity.roleName"
	})
	@ApiOperation("添加角色")
	public R<String> addRole(@RequestBody RoleEntity roleEntity){
		return userService.addRole(roleEntity);
	}

	/**
	 * 删除角色
	 */
	@PostMapping("removeRoleById")
	@ApiOperation("删除角色")
	public R<String> removeRoleById(@RequestParam String roleId){
		return roleService.removeRoleById(roleId);
	}

	/**
	 * 查询该用户下有哪些角色
	 */
	@PostMapping("findUserRoleList")
	@ApiOperation("查询该用户下有哪些角色")

	public R<List<UserRoleVo>> findUserRoleList(@RequestParam String userId){
		return userService.findUserRoleList(userId);
	}

	/**
	 * 修改角色名称
	 */
	@ApiOperation("修改角色名称")
	@PostMapping("updateRoleName")
	@ApiOperationSupport(includeParameters = {
		"roleEntity.roleId",
		"roleEntity.roleName"
	})
	public R<String> updateRoleName(@RequestBody RoleEntity roleEntity){
		return userService.updateRoleName(roleEntity);
	}

	/**
	 * 为用户批量绑定审批角色
	 */
	@ApiOperation("为用户批量绑定审批角色")
	@PostMapping("addUserRolesMapping")
	public R<String> addUserRolesMapping(@RequestParam String  userId,
										@RequestParam String[] roleIds){
		return userService.addUserRolesMapping(userId,roleIds);
	}

	/**
	 * 为用户删除已绑定的角色
	 */
	@ApiOperation("为用户删除已绑定的审批角色")
	@PostMapping("removeUserRoleMapping")
	public R<String> removeUserRoleMapping(@RequestParam String  userId,
										@RequestParam String roleId){
		return userService.removeUserRoleMapping(userId,roleId);
	}

	/**
	 * 同步用户表数据
	 */
	@ApiOperation("同步用户表数据")
	@PostMapping("syncUsersInfo")
	public R<Boolean> syncUsersInfo(){
		return R.data(userService.syncUsersInfonfo());
	}

	/**
	 * 同步用户表数据
	 */
	@ApiOperation("通过企业微信通讯录同步用户表数据")
	@PostMapping("syncUsersInfoByWeChat")
	public R<Boolean> syncUsersInfoByWeChat(){
		return R.data(userService.syncUsersInfoByWeChat());
	}

	@ApiOperation("为用户绑定关注事项")
	@PostMapping("/bindAttentionDeploy")
	@ApiOperationSupport(includeParameters = {
		"tbUserDeploymentEntity.userId",
		"tbUserDeploymentEntity.userDeploymentList.deployKey",
		"tbUserDeploymentEntity.userDeploymentList.attentionSql"
	})
	public R bindAttentionDeploy(@RequestBody TbUserDeploymentEntity tbUserDeploymentEntity){
		tbUserDeploymentService.bindAttentionDeploy(tbUserDeploymentEntity);
		return R.success("绑定成功");
	}

	@ApiOperation("获取用户关注事项")
	@GetMapping("/getUserDeployList")
	public R<List<TbUserDeploymentEntity>> getUserDeployList(@RequestParam("userId")Long userId){
		List<TbUserDeploymentEntity> userDeploymentEntityList = tbUserDeploymentService.getUserDeployList(userId);
		return R.data(userDeploymentEntityList);
	}

}
